home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 November / CPNL0711.ISO / boekhoud / finan / BADGER finance v1.0 beta 2.exe / xampplite / apache / bin / apxs.bat < prev    next >
DOS Batch File  |  2006-04-12  |  25KB  |  815 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!\xampplite\perl\bin\perl.exe -w
  14. #line 15
  15. # ====================================================================
  16. #
  17. #  Copyright 2003-2004  The Apache Software Foundation
  18. #
  19. #  Licensed under the Apache License, Version 2.0 (the "License");
  20. #  you may not use this file except in compliance with the License.
  21. #  You may obtain a copy of the License at
  22. #
  23. #      http://www.apache.org/licenses/LICENSE-2.0
  24. #
  25. #  Unless required by applicable law or agreed to in writing, software
  26. #  distributed under the License is distributed on an "AS IS" BASIS,
  27. #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. #  See the License for the specific language governing permissions and
  29. #  limitations under the License.
  30.  
  31. # apxs script designed to allow easy command line access to Apache
  32. # configuration parameters.
  33.  
  34. require 5.003;
  35. use strict;
  36. package apxs;
  37. use File::Copy;
  38. use File::Spec::Functions;
  39.  
  40. ##
  41. ##  Configuration
  42. ##
  43.  
  44. my %config_vars = ();
  45.  
  46. my $installbuilddir = '\xampplite\apache\build';
  47. get_config_vars("$installbuilddir/config_vars.mk",\%config_vars);
  48.  
  49. # read the configuration variables once
  50.  
  51. my $prefix          = get_vars('prefix');
  52. my $CFG_PREFIX      = $prefix;
  53. my $exec_prefix     = get_vars('exec_prefix');
  54. my $datadir         = get_vars('datadir');
  55. my $localstatedir   = get_vars('localstatedir');
  56. my $CFG_TARGET      = get_vars('progname');
  57. my $progname        = get_vars('progname');
  58. my $CFG_SYSCONFDIR  = get_vars('sysconfdir');
  59. my $CFG_SYSCONF     = get_vars('sysconf');
  60. my $CFG_CFLAGS      = join ' ', map { get_vars($_) }
  61.     qw(SHLTCFLAGS CFLAGS NOTEST_CPPFLAGS EXTRA_CPPFLAGS EXTRA_CFLAGS);
  62. my $includedir      = get_vars('includedir');
  63. my $CFG_INCLUDEDIR  = $includedir;
  64. my $libdir          = get_vars('libdir');
  65. my $CFG_LIBDIR      = $libdir;
  66. my $CFG_CC          = get_vars('CC');
  67. my $CFG_LD          = get_vars('LD');
  68. my $CFG_LDFLAGS     = get_vars('LDFLAGS');
  69. my $libexecdir      = get_vars('libexecdir');
  70. my $CFG_LIBEXECDIR  = $libexecdir;
  71. my $sbindir         = get_vars('sbindir');
  72. my $CFG_SBINDIR     = $sbindir;
  73. my $ltflags         = $ENV{LTFLAGS};
  74. my $apr_libname     = get_vars('APR_LIBNAME');
  75. my $aprutil_libname = get_vars('APRUTIL_LIBNAME');
  76.  
  77. $ltflags or $ltflags = '--silent';
  78.  
  79. my %internal_vars = map {$_ => 1}
  80.     qw(TARGET CC CFLAGS CFLAGS_SHLIB LD_SHLIB LDFLAGS_SHLIB LIBS_SHLIB
  81.        PREFIX SBINDIR INCLUDEDIR LIBEXECDIR SYSCONFDIR SYSCONF);
  82.  
  83. my $CP    = 'D:\perl\bin\perl.EXE -MExtUtils::Command -e cp';
  84. my $CHMOD = 'D:\perl\bin\perl.EXE -MExtUtils::Command -e chmod';
  85. my $RM_F  = 'D:\perl\bin\perl.EXE -MExtUtils::Command -e rm_f';
  86. my $TOUCH = 'D:\perl\bin\perl.EXE -MExtUtils::Command -e touch';
  87.  
  88. ##
  89. ##  parse argument line
  90. ##
  91.  
  92. #   defaults for parameters
  93. my $opt_n = '';
  94. my $opt_g = '';
  95. my $opt_c = 0;
  96. my $opt_o = '';
  97. my @opt_D = ();
  98. my @opt_I = ();
  99. my @opt_L = ();
  100. my @opt_l = ();
  101. my @opt_W = ();
  102. my @opt_S = ();
  103. my $opt_e = 0;
  104. my $opt_i = 0;
  105. my $opt_a = 0;
  106. my $opt_A = 0;
  107. my $opt_q = 0;
  108. my $opt_h = 0;
  109. my $opt_p = 0;
  110. my $opt_d = 0;
  111.  
  112. #   this subroutine is derived from Perl's getopts.pl with the enhancement of
  113. #   the "+" metacharacter at the format string to allow a list to be built by
  114. #   subsequent occurrences of the same option.
  115. sub Getopts {
  116.     my ($argumentative, @ARGV) = @_;
  117.     my $errs = 0;
  118.     local $_;
  119.     local $[ = 0;
  120.  
  121.     my @args = split / */, $argumentative;
  122.     while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
  123.         my ($first, $rest) = ($1,$2);
  124.         if ($_ =~ m|^--$|) {
  125.             shift @ARGV;
  126.             last;
  127.         }
  128.         my $pos = index($argumentative,$first);
  129.         if ($pos >= $[) {
  130.             if ($pos < $#args && $args[$pos+1] eq ':') {
  131.                 shift @ARGV;
  132.                 if ($rest eq '') {
  133.                     unless (@ARGV) {
  134.                         error("Incomplete option: $first (needs an argument)");
  135.                         $errs++;
  136.                     }
  137.                     $rest = shift(@ARGV);
  138.                 }
  139.                 eval "\$opt_$first = \$rest;";
  140.             }
  141.             elsif ($pos < $#args && $args[$pos+1] eq '+') {
  142.                 shift @ARGV;
  143.                 if ($rest eq '') {
  144.                     unless (@ARGV) {
  145.                         error("Incomplete option: $first (needs an argument)");
  146.                         $errs++;
  147.                     }
  148.                     $rest = shift(@ARGV);
  149.                 }
  150.                 eval "push(\@opt_$first, \$rest);";
  151.             }
  152.             else {
  153.                 eval "\$opt_$first = 1";
  154.                 if ($rest eq '') {
  155.                     shift(@ARGV);
  156.                 }
  157.                 else {
  158.                     $ARGV[0] = "-$rest";
  159.                 }
  160.             }
  161.         }
  162.         else {
  163.             error("Unknown option: $first");
  164.             $errs++;
  165.             if ($rest ne '') {
  166.                 $ARGV[0] = "-$rest";
  167.             }
  168.             else {
  169.                 shift(@ARGV);
  170.             }
  171.         }
  172.     }
  173.     return ($errs == 0, @ARGV);
  174. }
  175.  
  176. sub usage {
  177.     print STDERR <<'END';
  178. Usage: apxs -g [-S <var>=<val>] -n <modname>
  179.        apxs -q [-S <var>=<val>] <query> ...
  180.        apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]
  181.                [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]
  182.                [-Wl,<flags>] [-p] <files> ...
  183.        apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...
  184.        apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...
  185.  
  186. END
  187.     exit(1);
  188. }
  189.  
  190. #   option handling
  191. my $rc;
  192. ($rc, @ARGV) = &Getopts("qn:gco:I+D+L+l+W+S+eiaApd", @ARGV);
  193. &usage if ($rc == 0);
  194. &usage if ($#ARGV == -1 and not $opt_g);
  195. &usage if (not $opt_q and not ($opt_g and $opt_n) 
  196.            and not $opt_i and not $opt_c and not $opt_e);
  197.  
  198. #   argument handling
  199. my @args = @ARGV;
  200. my $name = 'unknown';
  201. $name = $opt_n if ($opt_n ne '');
  202.  
  203. if (@opt_S) {
  204.     my ($opt_S);
  205.     foreach $opt_S (@opt_S) {
  206.         if ($opt_S =~ m/^([^=]+)=(.*)$/) {
  207.             my ($var) = $1;
  208.             my ($val) = $2;
  209.             my $oldval = eval "\$CFG_$var";
  210.  
  211.             unless ($var and $oldval) {
  212.                 print STDERR "apxs:Error: no config variable $var\n";
  213.                 &usage;
  214.             }
  215.  
  216.             eval "\$CFG_${var}=\"${val}\"";
  217.         } else {
  218.             print STDERR "apxs:Error: malformatted -S option\n";
  219.             &usage;
  220.         }    
  221.     }
  222. }
  223.  
  224. ##
  225. ##  Initial shared object support check
  226. ##
  227. my $httpd = catfile get_vars("sbindir"), get_vars("progname");
  228. my $envvars = catfile get_vars("sbindir"), "envvars";
  229.  
  230. #allow apxs to be run from the source tree, before installation
  231. if ($0 =~ m:support/apxs$:) {
  232.     ($httpd = $0) =~ s:support/apxs$::;
  233. }
  234.  
  235. unless (-f $httpd) {
  236.     error("$httpd not found or not executable");
  237.     exit 1;
  238. }
  239.  
  240. sub get_config_vars{
  241.     my ($file, $rh_config) = @_;
  242.  
  243.     open IN, $file or die "cannot open $file: $!";
  244.     while (<IN>){
  245.         if (/^\s*(.*?)\s*=\s*(.*)$/){
  246.             $rh_config->{$1} = $2;
  247.         }
  248.     }
  249.     close IN;
  250. }
  251.  
  252. sub get_vars {
  253.     my $result = '';
  254.     my $ok = 0;
  255.     my $arg;
  256.     foreach $arg (@_) {
  257.         if (exists $config_vars{$arg} or exists $config_vars{lc $arg}) {
  258.             my $val = exists $config_vars{$arg}
  259.                 ? $config_vars{$arg}
  260.                     : $config_vars{lc $arg};
  261.             $val =~ s/[()]//g;
  262.             $result .= $val if defined $val;
  263.             $result .= ";;"; 
  264.             $ok = 1;
  265.         }
  266.         if (not $ok) {
  267.             if (exists $internal_vars{$arg} 
  268.                 or exists $internal_vars{lc $arg}) {
  269.                 my $val = exists $internal_vars{$arg} ? $arg : lc $arg;
  270.                 $val = eval "\$CFG_$val";
  271.                 $result .= $val if defined $val;
  272.                 $result .= ";;";
  273.                 $ok = 1;
  274.             }
  275.             if (not $ok) {
  276.                 error("Invalid query string `$arg'");
  277.                 exit(1);
  278.             }
  279.         }
  280.     }
  281.     $result =~ s|;;$||;
  282.     #    $result =~ s|:| |;
  283.     return $result;
  284. }
  285.  
  286. ##
  287. ##  Operation
  288. ##
  289.  
  290. #   helper function for executing a list of
  291. #   system command with return code checks
  292. sub execute_cmds {
  293.     my (@cmds) = @_;
  294.     my ($cmd, $rc);
  295.  
  296.     foreach $cmd (@cmds) {
  297.         notice($cmd);
  298.         $rc = system $cmd;
  299.         if ($rc) {
  300.             error(sprintf "Command failed with rc=%d\n", $rc << 8);
  301.             exit 1 ;
  302.         }
  303.     }
  304. }
  305.  
  306. if ($opt_g) {
  307.     ##
  308.     ##  SAMPLE MODULE SOURCE GENERATION
  309.     ##
  310.  
  311.     if (-d $name) {
  312.         error("Directory `$name' already exists. Remove first");
  313.         exit(1);
  314.     }
  315.  
  316.     my $data = join('', <DATA>);
  317.     $data =~ s!__END__.*!!s;
  318.     $data =~ s|%NAME%|$name|sg;
  319.     $data =~ s|%PROGNAME%|$progname|sg;
  320.     $data =~ s|%SYSCONF%|$CFG_SYSCONF|sg;
  321.     $data =~ s|%PREFIX%|$prefix|sg;
  322.     $data =~ s|%INSTALLBUILDDIR%|$installbuilddir|sg;
  323.  
  324.     my ($mkf, $src) = ($data =~ m|^(.+)-=\#=-\n(.+)|s);
  325.  
  326.     notice("Creating [DIR]  $name");
  327.     mkdir $name or die "Cannot mkdir $name: $!";
  328.     notice("Creating [FILE] $name/Makefile");
  329.     open(FP, ">${name}/Makefile") or die "Cannot open ${name}/Makefile: $!";
  330.     print FP $mkf;
  331.     close(FP);
  332.     notice("Creating [FILE] $name/mod_$name.c");
  333.     open(FP, ">${name}/mod_${name}.c") || die;
  334.     print FP $src;
  335.     close(FP);
  336.     notice("Creating [FILE] $name/.deps");
  337.     open(FP, ">${name}/.deps") or die "Cannot open ${name}/.deps: $!";
  338.     close(FP);
  339.  
  340.     exit(0);
  341. }
  342.  
  343. if ($opt_q) {
  344.     ##
  345.     ##  QUERY INFORMATION 
  346.     ##
  347.     my $result = get_vars(@args);
  348.     print "$result\n";
  349. }
  350.  
  351. my $apr_bindir = get_vars("APR_BINDIR");
  352. my $apu_bindir = get_vars("APU_BINDIR");
  353.  
  354. my $apr_includedir = qq{-I"$prefix/include"};
  355. my $apu_includedir = qq{-I$"prefix/include"};
  356.  
  357. if ($opt_c) {
  358.     ##
  359.     ##  SHARED OBJECT COMPILATION
  360.     ##
  361.     
  362.     #   split files into sources and objects
  363.     my @srcs = ();
  364.     my @objs = ();
  365.     my $f;
  366.     foreach $f (@args) {
  367.         if ($f =~ m|\.c$|) {
  368.             push(@srcs, $f);
  369.         }
  370.         else {
  371.             push(@objs, $f);
  372.         }
  373.     }
  374.  
  375.     #   determine output file
  376.     my $dso_file;
  377.     if ($opt_o eq '') {
  378.         if ($#srcs > -1) {
  379.             $dso_file = $srcs[0];
  380.             $dso_file =~ s|\.[^.]+$|.so|;
  381.         }
  382.         elsif ($#objs > -1) {
  383.             $dso_file = $objs[0];
  384.             $dso_file =~ s|\.[^.]+$|.so|;
  385.         }
  386.         else {
  387.             $dso_file = "mod_unknown.so";
  388.         }
  389.     }
  390.     else {
  391.         $dso_file = $opt_o;
  392.     }
  393.  
  394.     #   create compilation commands
  395.     my @cmds = ();
  396.     my $opt = '';
  397.     my ($opt_Wc, $opt_I, $opt_D);
  398.     foreach $opt_Wc (@opt_W) {
  399.         $opt .= "$1 " if ($opt_Wc =~ m|^\s*c,(.*)$|);
  400.     }
  401.     foreach $opt_I (@opt_I) {
  402.         $opt .= qq{ /I"$opt_I" };
  403.     }
  404.     foreach $opt_D (@opt_D) {
  405.         $opt .= qq{ /D "$opt_D" };
  406.     }
  407.     my $cflags = "$CFG_CFLAGS";
  408.     if ($opt_d) {
  409.         $cflags =~ s!NDEBUG!DEBUG!;
  410.         $cflags .= ' /Zi';
  411.         $cflags =~ s!/MD !/MDd !;
  412.     }
  413.     my $s;
  414.     my $mod;
  415.     foreach $s (@srcs) {
  416.         my $slo = $s;
  417.         $slo =~ s|\.c$|.slo|;
  418.         my $lo = $s;
  419.         $lo =~ s|\.c$|.lo|;
  420.         my $la = $s;
  421.         $la =~ s|\.c$|.la|;
  422.         my $o = $s;
  423.         $o =~ s|\.c$|.o|;
  424.         push(@cmds, qq{$CFG_CC $cflags -I"$CFG_INCLUDEDIR" $opt /c /Fo$lo $s});
  425.         unshift(@objs, $lo);
  426.     }
  427.  
  428.     #   create link command
  429.     my $o;
  430.     my $lo;
  431.     $opt = '';
  432.     foreach $o (@objs) {
  433.         $lo .= " $o";
  434.     }
  435.     my ($opt_Wl, $opt_L, $opt_l);
  436.     foreach $opt_Wl (@opt_W) {
  437.         if ($CFG_CC !~ m/gcc$/) {
  438.             $opt .= " $1" if ($opt_Wl =~ m|^\s*l,(.*)$|);
  439.         } else {
  440.             $opt .= " -W$opt_Wl";
  441.         }
  442.     }
  443.     foreach $opt_L (@opt_L) {
  444.         $opt .= qq{ /libpath:"$opt_L" };
  445.     }
  446.     foreach $opt_l (@opt_l) {
  447.         $opt_l .= '.lib' unless ($opt_l =~ /\.lib$/); 
  448.         $opt .= " $opt_l";
  449.     }
  450.  
  451.     if ($opt_p == 1) {
  452.         $opt .= " ".$aprutil_libname." ".$apr_libname;
  453.     }
  454.     else {
  455.         my $apr_ldflags;
  456.         $opt .= " $apr_ldflags";
  457.     }
  458.     my $ldflags = "$CFG_LDFLAGS";
  459.     if ($opt_d) {
  460.         $ldflags .= ' /debug';
  461.     }
  462.     push(@cmds, "$CFG_LD $ldflags /out:$dso_file $opt $lo");
  463.  
  464.     #   execute the commands
  465.     &execute_cmds(@cmds);
  466.  
  467.     #   allow one-step compilation and installation
  468.     if ($opt_i or $opt_e) {
  469.         @args = ( $dso_file );
  470.     }
  471. }
  472.  
  473. if ($opt_i or $opt_e) {
  474.     ##
  475.     ##  SHARED OBJECT INSTALLATION
  476.     ##
  477.     unless (-d $CFG_LIBEXECDIR) {
  478.         die "Directory $CFG_LIBEXECDIR not found";
  479.     }
  480.     #   determine installation commands
  481.     #   and corresponding LoadModule/AddModule directives
  482.     my @lmd = ();
  483.     my @amd = ();
  484.     my @cmds = ();
  485.     my $f;
  486.     foreach $f (@args) {
  487.         my $end = qr{(\.so|\.la)$};
  488.         if ($f !~ m!$end!) {
  489.             error("file $f is not a shared object");
  490.             exit(1);
  491.         }
  492.         my $t = $f;
  493.         $t =~ s|^.+/([^/]+)$|$1|;
  494.         $t =~ s|\.la$|\.so|;
  495.         (my $libf = $f) =~ s!$end!.lib!;
  496.         (my $libt = $t) =~ s!$end!.lib!;
  497.         (my $pdbf = $f) =~ s!$end!.pdb!;
  498.         (my $pdbt = $t) =~ s!$end!.pdb!;
  499.  
  500.         if ($opt_i) {
  501.             push(@cmds, "$CP $f $CFG_LIBEXECDIR");
  502.             push(@cmds, "$CHMOD 755 $CFG_LIBEXECDIR\\$t");
  503.             if (-f $libf) {
  504.                 push(@cmds, "$CP $libf $CFG_LIBDIR");
  505.                 push(@cmds, "$CHMOD 755 $CFG_LIBDIR\\$libt");
  506.             }
  507.             if ($opt_d and -f $pdbf) {
  508.                 push(@cmds, "$CP $pdbf $CFG_LIBEXECDIR");
  509.                 push(@cmds, "$CHMOD 755 $CFG_LIBEXECDIR\\$pdbt");
  510.             }
  511.         }
  512.  
  513.         #   determine module symbolname and filename
  514.         my $filename = '';
  515.         if ($name eq 'unknown') {
  516.             $name = '';
  517.             my $base = $f;
  518.             $base =~ s|\.[^.]+$||;
  519.             if (-f "$base.c") {
  520.                 open(FP, "<$base.c");
  521.                 my $content = join('', <FP>);
  522.                 close(FP);
  523.                 if ($content =~ m|.*module\s+(?:AP_MODULE_DECLARE_DATA\s+)?([a-zA-Z0-9_]+)_module\s*=\s*.*|s) {
  524.                     $name = "$1";
  525.                     $filename = "$base.c";
  526.                     $filename =~ s|^[^/]+/||;
  527.                 }
  528.             }
  529.             if ($name eq '') {
  530.                 if ($base =~ m|.*mod_([a-zA-Z0-9_]+)\..+|) {
  531.                     $name = "$1";
  532.                     $filename = $base;
  533.                     $filename =~ s|^[^/]+/||;
  534.                 }
  535.             }
  536.             if ($name eq '') {
  537.                 error("Sorry, cannot determine bootstrap symbol name");
  538.                 error("Please specify one with option `-n'");
  539.                 exit(1);
  540.             }
  541.         }
  542.         if ($filename eq '') {
  543.             $filename = "mod_${name}.c";
  544.         }
  545.         my $dir = $CFG_LIBEXECDIR;
  546.         $dir =~ s|^$CFG_PREFIX/?||;
  547.         $dir =~ s|(.)$|$1/|;
  548.         $dir =~ s|\\|/|g;
  549.         $t =~ s|\.la$|.so|;
  550.         push(@lmd, 
  551.              sprintf("LoadModule %-18s %s", "${name}_module", qq{"$dir$t"}));
  552.         push(@amd, sprintf("AddModule %s", $filename));
  553.     }
  554.  
  555.     #   execute the commands
  556.     &execute_cmds(@cmds);
  557.  
  558.     #   activate module via LoadModule/AddModule directive
  559.     if ($opt_a or $opt_A) {
  560.         if (not -f "$CFG_SYSCONFDIR/$CFG_SYSCONF") {
  561.             error("Config file $CFG_SYSCONFDIR/$CFG_SYSCONF not found");
  562.             exit(1);
  563.         }
  564.  
  565.         open(FP, "<$CFG_SYSCONFDIR/$CFG_SYSCONF") 
  566.             || die "Cannot open $CFG_SYSCONFDIR/$CFG_SYSCONF: $!";
  567.         my $content = join('', <FP>);
  568.         close(FP);
  569.  
  570.         if ($content !~ m|\n\#?\s*LoadModule\s+|) {
  571.             error("Activation failed for custom $CFG_SYSCONFDIR/$CFG_SYSCONF file.");
  572.             error("At least one `LoadModule' directive already has to exist.");
  573.             exit(1);
  574.         }
  575.  
  576.         my $lmd;
  577.         my $c = '';
  578.         $c = '#' if ($opt_A);
  579.         foreach $lmd (@lmd) {
  580.             my $what = $opt_A ? "preparing" : "activating";
  581.             if ($content !~ m|\n\#?\s*$lmd|) {
  582.                 # check for open <containers>, so that the new LoadModule
  583.                 # directive always appears *outside* of an <container>.
  584.  
  585.                 my $before = 
  586.                     ($content =~ m|^(.*\n)\#?\s*LoadModule\s+[^\n]+\n|s)[0];
  587.  
  588.                 # the '()=' trick forces list context and the scalar
  589.                 # assignment counts the number of list members (aka number
  590.                 # of matches) then
  591.                 my $cntopen = () = ($before =~ m|^\s*<[^/].*$|mg);
  592.                 my $cntclose = () = ($before =~ m|^\s*</.*$|mg);
  593.  
  594.                 if ($cntopen == $cntclose) {
  595.                     # fine. Last LoadModule is contextless.
  596.                     $content =~ s|^(.*\n\#?\s*LoadModule\s+[^\n]+\n)|$1$c$lmd\n|s;
  597.                 }
  598.                 elsif ($cntopen < $cntclose) {
  599.                     error('Configuration file is not valid. There are sections'
  600.                           . ' closed before opened.');
  601.                     exit(1);
  602.                 }
  603.                 else {
  604.                     # put our cmd after the section containing the last
  605.                     # LoadModule.
  606.                     my $found =
  607.                         $content =~ s!\A (  # string and capture start
  608.                                           (?:(?:
  609.                                               ^\s*  # start of conf line with a
  610.                                               (?:[^<]|<[^/]) # directive which
  611.                                                              # does not
  612.                                                              # start with '</'
  613.                                               
  614.                                               .*(?:$)\n  # rest of the line.
  615.                                                  # the '$' is in parentheses
  616.                                                  # to avoid misinterpreting
  617.                                                  # the string "$\" as
  618.                                                  # perl variable.
  619.                                                  
  620.                                                 )* # catch as much as possible
  621.                                                    # of such lines. (including
  622.                                                    # zero)
  623.                                               
  624.                                               ^\s*</.*(?:$)\n? 
  625.                                                    # after the above, we
  626.                                                    # expect a config line with
  627.                                                    # a closing container (</)
  628.                                                        
  629.                                                       ) {$cntopen}       
  630.                                               # the whole pattern (bunch
  631.                                               # of lines that end up with
  632.                                               # a closing directive) must
  633.                                               # be repeated $cntopen
  634.                                               # times. That's it.
  635.                                               # Simple, eh? ;-)
  636.                                     
  637.                                              )  # capture end  
  638.                                            !$1$c$lmd\n!mx;  
  639.                     unless ($found) {
  640.                         error('Configuration file is not valid. There are '
  641.                               . 'sections opened and not closed.');
  642.                         exit(1);
  643.                     }
  644.                 }
  645.             }
  646.             else {
  647.                 # replace already existing LoadModule line
  648.                 $content =~ s|^(.*\n)\#?\s*$lmd[^\n]*\n|$1$c$lmd\n|s;
  649.             }
  650.             $lmd =~ m|LoadModule\s+(.+?)_module.*|;
  651.             notice("[$what module `$1' in $CFG_SYSCONFDIR\\$CFG_SYSCONF]");
  652.         } 
  653.         my $amd;
  654.         foreach $amd (@amd) {
  655.             if ($content !~ m|\n\#?\s*$amd|) {
  656.                 $content =~ s|^(.*\n\#?\s*AddModule\s+[^\n]+\n)|$1$c$amd\n|sg;
  657.             } 
  658.             else {
  659.                 $content =~ s|^(.*\n)\#?\s*$amd[^\n]*\n|$1$c$amd\n|sg;
  660.             }
  661.         }
  662.  
  663.         if (@lmd or @amd) {
  664.             my $conf = catfile $CFG_SYSCONFDIR, $CFG_SYSCONF;
  665.             my $conf_new = $conf . '.new';
  666.             my $conf_bak = $conf . '.bak';
  667.             if (open(FP, '>', $conf_new)) {
  668.                 print FP $content;
  669.                 close(FP);
  670.                 copy($conf, $conf_bak) or
  671.                          die "Backup of $conf failed: $!";
  672.                 copy($conf_new, $conf) or
  673.                          die "Copying $conf_new to $conf failed: $!";
  674.                 unlink $conf_new or
  675.                     die "Removing $conf_new failed: $!";
  676.             } 
  677.             else {
  678.                 notice("unable to open configuration file $conf_new");
  679.             }
  680.         }
  681.     }
  682. }
  683.  
  684. sub error{
  685.     print STDERR "apxs:Error: $_[0].\n";
  686. }
  687.  
  688. sub notice{
  689.     print STDERR "$_[0]\n";
  690. }
  691.  
  692. ##EOF##
  693. __DATA__
  694. ##
  695. ##  Makefile -- Build procedure for sample %NAME% Apache module
  696. ##  Autogenerated via ``apxs -n %NAME% -g''.
  697. ##
  698.  
  699. builddir=.
  700. top_srcdir=%PREFIX%
  701. top_builddir=%PREFIX%
  702.  
  703. #   the used tools
  704. APXS=apxs
  705. APACHECTL=%PROGNAME% -k
  706.  
  707. #   additional defines, includes and libraries
  708. #DEFS=-Dmy_define=my_value
  709. #INCLUDES=-Imy/include/dir
  710. #LIBS=-Lmy/lib/dir -lmylib
  711.  
  712. #   the default target
  713. all: local-shared-build
  714.  
  715. #   install the shared object file into Apache 
  716. install: install-modules
  717.  
  718. #   cleanup
  719. clean:
  720.         -@erase mod_%NAME%.lo mod_%NAME%.ilk mod_%NAME%.so mod_%NAME%.lib mod_%NAME%.exp mod_%NAME%.pdb
  721.  
  722. #   simple test
  723. test: reload
  724.     GET http://localhost/%NAME%
  725.  
  726. #   install and activate shared object by reloading Apache to
  727. #   force a reload of the shared object file
  728. reload: install restart
  729.  
  730. #   the general Apache start/restart/stop
  731. #   procedures
  732. start:
  733.     $(APACHECTL) start
  734. restart:
  735.     $(APACHECTL) restart
  736. stop:
  737.     $(APACHECTL) stop
  738.  
  739. -=#=-
  740. /* 
  741. **  mod_%NAME%.c -- Apache sample %NAME% module
  742. **  [Autogenerated via ``apxs -n %NAME% -g'']
  743. **
  744. **  To play with this sample module first compile it into a
  745. **  DSO file and install it into Apache's modules directory 
  746. **  by running:
  747. **
  748. **    $ apxs -c -i mod_%NAME%.c
  749. **
  750. **  Then activate it in Apache's %SYSCONF% file for instance
  751. **  for the URL /%NAME% in as follows:
  752. **
  753. **    #   %SYSCONF%
  754. **    LoadModule %NAME%_module modules/mod_%NAME%.so
  755. **    <Location /%NAME%>
  756. **    SetHandler %NAME%
  757. **    </Location>
  758. **
  759. **  Then after restarting Apache via
  760. **
  761. **    $ apachectl restart
  762. **
  763. **  you immediately can request the URL /%NAME% and watch for the
  764. **  output of this module. This can be achieved for instance via:
  765. **
  766. **    $ lynx -mime_header http://localhost/%NAME% 
  767. **
  768. **  The output should be similar to the following one:
  769. **
  770. **    HTTP/1.1 200 OK
  771. **    Date: Tue, 31 Mar 1998 14:42:22 GMT
  772. **    Server: Apache/1.3.4 (Unix)
  773. **    Connection: close
  774. **    Content-Type: text/html
  775. **  
  776. **    The sample page from mod_%NAME%.c
  777. */ 
  778.  
  779. #include "httpd.h"
  780. #include "http_config.h"
  781. #include "http_protocol.h"
  782. #include "ap_config.h"
  783.  
  784. /* The sample content handler */
  785. static int %NAME%_handler(request_rec *r)
  786. {
  787.     if (strcmp(r->handler, "%NAME%")) {
  788.         return DECLINED;
  789.     }
  790.     r->content_type = "text/html";      
  791.  
  792.     if (!r->header_only)
  793.         ap_rputs("The sample page from mod_%NAME%.c\n", r);
  794.     return OK;
  795. }
  796.  
  797. static void %NAME%_register_hooks(apr_pool_t *p)
  798. {
  799.     ap_hook_handler(%NAME%_handler, NULL, NULL, APR_HOOK_MIDDLE);
  800. }
  801.  
  802. /* Dispatch list for API hooks */
  803. module AP_MODULE_DECLARE_DATA %NAME%_module = {
  804.     STANDARD20_MODULE_STUFF, 
  805.     NULL,                  /* create per-dir    config structures */
  806.     NULL,                  /* merge  per-dir    config structures */
  807.     NULL,                  /* create per-server config structures */
  808.     NULL,                  /* merge  per-server config structures */
  809.     NULL,                  /* table of config file commands       */
  810.     %NAME%_register_hooks  /* register hooks                      */
  811. };
  812.  
  813. __END__
  814. :endofperl
  815.